/**
 *
 */
package com.angel.data.generator.base;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;

import com.angel.dao.generic.factory.DAOFactory;
import com.angel.data.generator.exceptions.DataGeneratorException;
import com.angel.data.generator.interfaces.DataGenerator;

/**
 * @author William
 * @since 15/January/2009.
 *
 */
public class DataClassGenerator{

	private static final Logger LOGGER = Logger.getLogger(DataClassGenerator.class);

	private DataGenerator dataGenerator;

	private Collection<Object> dataGenerated;

	public DataClassGenerator(DataGenerator dataGenerator){
		super();
		this.dataGenerator = dataGenerator;
	}

	/**
	 * @return the dataGenerator
	 */
	public DataGenerator getDataGenerator() {
		return dataGenerator;
	}

	/**
	 * @return the dataGenerated
	 */
	public Collection<Object> getDataGenerated() {
		return dataGenerated;
	}

	@SuppressWarnings("unchecked")
	public <T> Collection<T> generateData(DAOFactory daoFactory){
		this.dataGenerated = new ArrayList<Object>();
		dataGenerator.generateData(this.dataGenerated, daoFactory);
		try {
			for(Object o: this.dataGenerated){
				this.dataGenerator.getGenericDAO().persist(o);
			}
		} catch(Exception e){
			throw new DataGeneratorException("An error occurred persisting data.", e);
		}
		return (Collection<T>) this.dataGenerated;
	}

	public Class<? extends DataGenerator>[] getDependencies(){
		return this.dataGenerator.getDependecesGenerators();
	}

	public boolean isForDataGenerator(Class<? extends DataGenerator> dependencyClass) {
		return this.getDataGeneratorClass().equals(dependencyClass);
	}

	@SuppressWarnings("unchecked")
	public Class<? extends DataGenerator> getDataGeneratorClass() {
		return (Class<? extends DataGenerator>) this.getDataGenerator().thisDataGeneratorClass();
	}

	public boolean dependsOn(Class<? extends DataGenerator> dataGeneratorClass) {
		boolean dependsOn = false;
		if(this.hasDependencies() && (this.getDependencies() != null || this.quantityDependencies() > 0)){
			for(Class<? extends DataGenerator> dataGeneratorDependencyClass: this.getDependencies()){
				if(dataGeneratorDependencyClass.equals(dataGeneratorClass)){
					LOGGER.info("[" + this.getDataGeneratorClassSimpleName() + "] DEPEDENS on [" + dataGeneratorClass.getSimpleName() + "].");
					dependsOn = true;
					break;
				} else {
					LOGGER.info("[" + this.getDataGeneratorClassSimpleName() + "] NOT depends on [" + dataGeneratorClass.getSimpleName() + "].");
				}
			}
		}
		return dependsOn;
	}

	public int quantityDependencies(){
		return this.getDependencies() != null ? this.getDependencies().length : 0;
	}

	protected void deleteData(){
		this.getDataGenerator().getGenericDAO().deleteAll();
	}

	public boolean hasDependencies() {
		return this.quantityDependencies() > 0;
	}

	public String getDataGeneratorClassFullCanonicalName() {
		return this.getDataGenerator().thisDataGeneratorClass().getCanonicalName() + " [" + this.getDataGenerator().getDataGeneratorClass().getCanonicalName() + "].";
	}

	public String getDataGeneratorClassFullSimpleName() {
		return this.getDataGenerator().thisDataGeneratorClass().getSimpleName() + " [" + this.getDataGenerator().getDataGeneratorClass().getSimpleName() + "].";
	}

	public String getDataGeneratorClassSimpleName() {
		return this.getDataGenerator().thisDataGeneratorClass().getSimpleName();
	}

	public String getDataGeneratorClassCanonicalName() {
		return this.getDataGenerator().thisDataGeneratorClass().getCanonicalName();
	}

	public String getDomainObjectClassName() {
		return this.getDataGeneratorClass().getCanonicalName();
	}
}
